package com.sanjiahantang.ythshopping.utils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.adapter.home.PayTypeAdapter;
import com.sanjiahantang.ythshopping.model.AliPatModel;
import com.sanjiahantang.ythshopping.model.OrderInfor;
import com.sanjiahantang.ythshopping.model.WXpayModel;
import com.sanjiahantang.ythshopping.presenter.OrderPopPayPresenter;
import com.sanjiahantang.ythshopping.presenter.PayActivePresenter;
import com.sanjiahantang.ythshopping.views.OrderPopPayView;
import com.sanjiahantang.ythshopping.views.PayActiveView;
import com.umeng.socialize.facebook.controller.utils.ToastUtil;

/**
 * Created by admin on 2017/8/15.
 */

public class PayTypePopuwindow extends PopupWindow implements OrderPopPayView,PayActiveView {

    View view,iv_paytype_close;
    ListView list;
    Button btn_paysubmit;
    int paytypenum=-1;
    int paytypeid;
    OrderPopPayPresenter presenter;
    String payname;
    String aliinfo="";
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_CHECK_FLAG = 2;
    AliCask back;
    PayActivePresenter paypresenter;
    Activity context;

    public  PayTypePopuwindow(final Activity context, final OrderInfor model ,AliCask back)
    {

        this.back=back;
        LayoutInflater inflater=LayoutInflater.from(context);
        view=inflater.inflate(R.layout.pop_order_paytype,null);

        for(int i=0;i<model.getPayment_list().size();i++)
        {
            model.getPayment_list().get(i).setChecked(false);
        }

        this.context=context;
        this.setContentView(view);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(context.getWindowManager().getDefaultDisplay().getHeight()/2+50);
        this.setFocusable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setOutsideTouchable(true);

        final PayTypeAdapter paytypeadapter = new PayTypeAdapter(context, R.layout.paytype_item);

        presenter=new OrderPopPayPresenter(context,this,model.getOrder_id());

        list= (ListView) view.findViewById(R.id.lv_paytype_list);
        iv_paytype_close = view.findViewById(R.id.iv_paytype_close);
        list.setAdapter(paytypeadapter);

        paytypeadapter.addAll(model.getPayment_list());

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (paytypenum < 0) {
                    paytypeadapter.clear();
                    paytypenum = position;
                    model.getPayment_list().get(position).setChecked(true);
                    paytypeid = model.getPayment_list().get(position).getPay_id();
                    payname=model.getPayment_list().get(position).getPay_name();
                    paytypeadapter.addAll(model.getPayment_list());

                } else {
                    if (!model.getPayment_list().get(position).isChecked()) {
                        paytypeadapter.clear();
                        model.getPayment_list().get(paytypenum).setChecked(false);
                        paytypenum = position;
                        model.getPayment_list().get(position).setChecked(true);
                        paytypeid = model.getPayment_list().get(position).getPay_id();
                        payname=model.getPayment_list().get(position).getPay_name();
                        paytypeadapter.addAll(model.getPayment_list());
                    }

                }

            }
        });

        btn_paysubmit= (Button) view.findViewById(R.id.btn_paytype_typetrue);
        btn_paysubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model.getPay_set().equals("1"))
                {
                    if (payname.equals("支付宝全球支付"))
                    {
                        presenter.LoadALiPayMsg(model.getPay_set(),String.valueOf(paytypeid));
                    }
                }else {
                    if (paytypeid==1)
                    {
                        presenter.LoadALiPayMsg(model.getPay_set(),String.valueOf(paytypeid));
                    }

                    if (payname.equals("微信支付"))
                    {
                        presenter.LoadWXPayMsg(model.getPay_set(),String.valueOf(paytypeid));
                    }
                }

            }
        });

        iv_paytype_close.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                PayTypePopuwindow.this.dismiss();
            }
        });


    }

    @Override
    public void reWXinfo(WXpayModel wxpay) {
        this.dismiss();
        back.reWX(wxpay);
    }

    @Override
    public void reALiinfo(AliPatModel ali) {

        this.dismiss();
        if(ali == null ){
            ToastUtil.showToast(context,"错误");
            return;
        }
        back.reAli(ali.getData().getPayinfo());

    }

    @Override
    public void getWXinfo(WXpayModel wxpay) {

    }

    @Override
    public void getALiinfo(AliPatModel ali) {

    }

    public interface AliCask{
        void reAli(String ali) ;
        void reWX(WXpayModel wxpay);
    }


}
